<template>
	<view>
		<view>
			<!-- 背景色区域 -->
			<view class="titleNview-background"
				style="background: url('../../static/wximg/bjTop2.png'); background-size: 100%;">
				<!-- v-if="!noticeShow" anchor wos 会影响整体布局 -->

				<view class="title-logo">
					<image src="../../static//wximg/logo_w.png"></image>
				</view>
			</view>

			<view class="main">
				<!-- <view class="item wos-flex wos-flex-row wos-flex-between" @tap="clearCache">
					<view>清理缓存</view>
					<view>{{fileSizeString}}</view>
				</view> -->
				<view class="item-sm  wos-flex wos-flex-row wos-flex-between">
					<view class="item-sm-title">
						<view v-if="ifData">
							使用数据网络
						</view>
						<view v-else>
							使用物联网数据
						</view>
					</view>
					<view>
						<view v-if="ifData">
							<switch color="#007457" checked @change="switch1Change" />
						</view>
						<view v-else>
							<switch color="#007457" @change="switch1Change" />
						</view>
					</view>
				</view>
				<view class="item wos-flex wos-flex-row wos-flex-between" @tap="clearStroage">
					<view>放弃离线数据</view>
					<view></view>
				</view>
				<view class="item wos-flex wos-flex-row wos-flex-between" @tap="goGengxin">
					<view>查看更新内容</view>
					<view></view>
				</view>
			</view>
		</view>
		<TabBar v-if="onshow" :tabIndex="tabindex"></TabBar>
	</view>
</template>

<script>
	import TabBar from '@/components/tabBar.vue'
	export default {
		components: {
			TabBar
		},
		data() {
			return {
				tabindex: 3,
				onshow: true,
				fileSizeString: 0,
				ifData: true
			}
		},
		mounted() {
			this.ifData = uni.getStorageSync('netType')
			this.formatSize()
		},
		methods: {
			goGengxin(){
				uni.navigateTo({
					url:'/pages/gengxin/gengxin'
				})
			},
			clearStroage() {
				uni.showModal({
					title: '提示',
					content: '是否要清除缓存',
					success: async (res) => {
						if (res.confirm) {
							console.log("ok")
							getApp().globalData.ifTaskTimer = false
							if (uni.getStorageSync('catch')){
								let allShopInfo = uni.getStorageSync('catch').data.map.allShopInfo
								for (let i in allShopInfo) {
									let e = allShopInfo[i]
									if (e.deliveryStatus === 5) {
										e.task.receiveUserName = uni.getStorageSync(e.task.stroageName) //
										await undateTaskImg(e.task).then(res => {
											if (res.code == 200) {
												let tmp = uni.getStorageSync('catch')
												uni.removeStorageSync(e.task.receiveUserName) //
												tmp.data.map.allShopInfo[e.i].task = ''
												tmp.data.map.allShopInfo[e.i].deliveryStatus = 6
												uni.setStorageSync('catch', tmp)
											}
										})
									}
								}
								uni.removeStorageSync("dictionaryList")
								uni.removeStorageSync("catch")
								uni.removeStorageSync("catchAll")
								uni.removeStorageSync("catchStatus")
								this.clearCache()
								uni.showToast({
								    title: '已经清理离线缓存数据',
								    duration: 2000,
								});
							}
						}
					}
				})
			},
			clearCache() {
				let that = this;
				let os = plus.os.name;
				if (os == 'Android') {
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, "listFiles");
					let len = files.length;
					for (let i = 0; i < len; i++) {
						let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径  
						plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
							if (entry.isDirectory) {
								entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录  
									uni.showToast({
										title: '缓存清理完成',
										duration: 2000
									});
									that.formatSize(); // 重新计算缓存  
								}, function(e) {
									console.log(e.message)
								});
							} else {
								entry.remove();
							}
						}, function(e) {
							console.log('文件路径读取失败')
						});
					}
				} else { // ios暂时未找到清理缓存的方法，以下是官方提供的方法，但是无效，会报错  
					plus.cache.clear(function() {
						uni.showToast({
							title: '缓存清理完成',
							duration: 2000
						});
						that.formatSize();
					});
				}
			},
			switch1Change(e) {
				this.ifData = e.target.value
				// console.log(e.target.value)
				uni.setStorageSync('netType', e.target.value)
			},
			formatSize() {
				let that = this;
				plus.cache.calculate(function(size) {
					let sizeCache = parseInt(size);
					if (sizeCache == 0) {
						that.fileSizeString = "0B";
					} else if (sizeCache < 1024) {
						that.fileSizeString = sizeCache + "B";
					} else if (sizeCache < 1048576) {
						that.fileSizeString = (sizeCache / 1024).toFixed(2) + "KB";
					} else if (sizeCache < 1073741824) {
						that.fileSizeString = (sizeCache / 1048576).toFixed(2) + "MB";
					} else {
						that.fileSizeString = (sizeCache / 1073741824).toFixed(2) + "GB";
					}
				});
			}
		}
	}
</script>

<style scoped>
	switch {
		transform: scale(0.6);
	}

	.main {}

	.item {
		padding: 32rpx;
		border-bottom: 1rpx solid #c7cccb;
	}

	.item-sm {
		padding: 20rpx 32rpx;
		border-bottom: 1rpx solid #c7cccb;

	}

	.item-sm-title {
		padding-top: 15rpx;
	}

	.titleNview-background {
		width: 100%;
		height: 258upx;
		-webkit-transition: .4s;
		transition: .4s;
		/* background: url(../../static/font/temp/bjTop.png); */
		background-size: 100% 100%;
	}

	.title-logo {
		padding: 134upx 0;
		width: 750rpx;
		text-align: center;
	}

	.title-logo image {
		width: 248upx;
		height: 76upx;
	}



	.wos-flex {
		display: flex;
	}

	.wos-flex-row {
		flex-direction: row;
	}

	.wos-flex-column {
		flex-direction: column
	}

	.wos-flex-column-reverse {
		flex-direction: column-reverse
	}

	.wos-flex-center {
		justify-content: center;
		align-items: center
	}

	.wos-flex-between {
		justify-content: space-between;
	}

	.wos-flex-warp {
		flex-wrap: wrap
	}

	.wos-flex-nowrap {
		flex-wrap: nowrap;
	}

	.wos-flex-auto {
		/* flex: auto; */
		flex: 1
	}

	.wos-flex-right {
		justify-content: flex-end
	}
</style>
